<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use Config;
use app\common\util\DBUtil;
use util\IpUtil;

class Admin extends Model
{
    protected $table = "admin";
    public function _construct()
    {
        $this->$connection=config('admin_db');
    }
    
    public function is_login()
    {
        $user = session('user');
        if (empty($user)) {
            if (cookie("?uid") && cookie("?sign_token")) {                
                $uid = $cookie("uid");
                $user = DBUtil::connect_admin()->table('admin_user')->where('id', $uid)->find();
                if ($user) {
                    $sign_token = data_signature($user['username'].$user['id'].$user['last_login_time']);
                    if (cookie('sign_token') == $sign_token) {
                        $this->auto_login($user, true);
                        return $user['id'];
                    }
                }
            }
            // var_dump("empty");
            return 0;
        } else {
            // var_dump($user['uid']);
            return session('user_sign') == data_signature($user) ? $user['uid'] : 0;
        }
    }

    public function login($username = '', $password = '', $rememberme = false)
    {
        $userinfo = DBUtil::connect_admin()->table('admin_user')->where('username', $username)->find();
        if (!$userinfo) {
            $this->error = '账号不存在';
            return false;
        }
        if ($userinfo['is_disabled'] > 0) {
            $this->error = '账号被禁用';
            return false;
        }

        $lastLoginTime = date('Y-m-d H:i:s');
        $update_date =['last_login_time'   => $lastLoginTime,
                        'last_login_ip'     => request()->ip(),
                        'login_count'       => $userinfo['login_count'] + 1,
                        'last_addr'         => IpUtil::ip2addr(request()->ip())
            ];
        DBUtil::connect_admin()->table('admin_user')->where('id', $userinfo['id'])->update($update_date);
        $userinfo['last_login_ip'] = request()->ip();
        $userinfo['last_login_time'] = $lastLoginTime;

        $this->auto_login($userinfo, $rememberme);
        return true;
    }
    
    public function getUserGroupList($uid)
    {
        $db = DBUtil::connect_admin();
        $list = $db->table('admin_user_access')->where('uid', $uid)->where('platform', config('platform'))->column('groupid');
        return $list;
    }

    

    public function auto_login($user, $rememberme = false)
    {
        $auth = array(
            "uid"               =>  $user['id'],
            "last_login_ip"     =>  $user['last_login_ip'],
            "last_login_time"   =>  $user['last_login_time'],
            "username"          =>  $user['username'],
            'groupid'           =>  $user['groupid']
        );
        session('uid', $user['id']);
        session('user', $auth);
        session("user_sign", data_signature($auth));

        if ($rememberme) {
            $sign_token = $user['username'].$user['id'].$user['last_login_time'] ;
            cookie('$uid', $user['id'], 24 * 3600 * 7);
            cookie('sign_token', data_signature($sign_token), 24 * 3600 * 7);
        }
    }
}
